package com.ruoyi.products.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.products.domain.BussCoupons;
import com.ruoyi.products.mapper.BussCouponsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.products.mapper.BussDownloadMapper;
import com.ruoyi.products.domain.BussDownload;
import com.ruoyi.products.service.IBussDownloadService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 下载记录Service业务层处理
 *
 * @author star
 * @date 2025-06-17
 */
@Service
public class BussDownloadServiceImpl implements IBussDownloadService {
    @Autowired
    private BussDownloadMapper bussDownloadMapper;
    @Autowired
    private BussCouponsMapper bussCouponsMapper;

    /**
     * 查询下载记录
     *
     * @param id 下载记录主键
     * @return 下载记录
     */
    @Override
    public BussDownload selectBussDownloadById(Long id) {
        return bussDownloadMapper.selectBussDownloadById(id);
    }

    /**
     * 查询下载记录列表
     *
     * @param bussDownload 下载记录
     * @return 下载记录
     */
    @Override
    public List<BussDownload> selectBussDownloadList(BussDownload bussDownload) {
        return bussDownloadMapper.selectBussDownloadList(bussDownload);
    }

    /**
     * 新增下载记录
     *
     * @param bussDownload 下载记录
     * @return 结果
     */
    @Override
    public int insertBussDownload(BussDownload bussDownload) {
        bussDownload.setCreateTime(DateUtils.getNowDate());
        return bussDownloadMapper.insertBussDownload(bussDownload);
    }

    /**
     * 修改下载记录
     *
     * @param bussDownload 下载记录
     * @return 结果
     */
    @Override
    public int updateBussDownload(BussDownload bussDownload) {
        return bussDownloadMapper.updateBussDownload(bussDownload);
    }

    /**
     * 批量删除下载记录
     *
     * @param ids 需要删除的下载记录主键
     * @return 结果
     */
    @Override
    public int deleteBussDownloadByIds(Long[] ids) {
        return bussDownloadMapper.deleteBussDownloadByIds(ids);
    }

    /**
     * 删除下载记录信息
     *
     * @param id 下载记录主键
     * @return 结果
     */
    @Override
    public int deleteBussDownloadById(Long id) {
        return bussDownloadMapper.deleteBussDownloadById(id);
    }

    @Override
    @Transactional
    public AjaxResult checkDownloadCount(String productsId) {

        //查询用户当前的套餐情况
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //回去当前用户信息
        SysUser user = loginUser.getUser();
        Long couponsId = user.getCouponsId();

        //当前用户的套餐信息
        BussCoupons userCoupons = bussCouponsMapper.selectBussCouponsById(couponsId);
        //当前下载记录
        List<BussDownload> bussDownloads = bussDownloadMapper.checkDownloadCount(loginUser.getUserId());

        //校验是否 已下载过
        if (bussDownloads.size() > 0) {
            //判断是否已下载
            for (BussDownload bussDownload : bussDownloads) {
                //如果包含返回成功
                if (bussDownload.getProductsId().equals(productsId)) {
                    return  AjaxResult.success("验证成功");
                }
            }
        }


        //如果下载超过下载次数 下载已超过限制
        if (bussDownloads.size() > 0 && bussDownloads.size() >= userCoupons.getDayCount()) {
            return AjaxResult.error("下载已超过限制，下载素材不可超过"+userCoupons.getDayCount()+"件 素材,请升级套餐");
        }

        //否则直接插入
        BussDownload bussDownload = new BussDownload();
        bussDownload.setUserId(loginUser.getUserId());
        bussDownload.setProductsId(productsId);
        //如果不包含 插入
        insertBussDownload(bussDownload);
        return  AjaxResult.success("购买成功");
    }

    @Override
    public Map getDownloadCount() {
        HashMap<String, Object> resultMap = new HashMap<>();
        //查询用户当前的套餐情况
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //回去当前用户信息
        SysUser user = loginUser.getUser();
        Long couponsId = user.getCouponsId();
        //查询套餐信息
        BussCoupons userCoupons = bussCouponsMapper.selectBussCouponsById(couponsId);

        List<BussDownload> bussDownloads = bussDownloadMapper.checkDownloadCount(loginUser.getUserId());

        resultMap.put("userCount", bussDownloads.size()); //已下载次数
        resultMap.put("downloadCount", userCoupons.getDayCount());  //当日下载次数
        return resultMap;
    }
}
